// 77. 组合
/**
 * @param {number} n
 * @param {number} k
 * @return {number[][]}
 */
var combine = function (n, k) {
    let arr = new Array(n).fill(0).map((_, i) => i + 1);
    let ret = [];
    for (let i = 0; i < arr.length; i++) {
        dfs(arr, k, i, 0, [], ret);
    }

    console.log(JSON.stringify(ret));
};

function dfs(arr, max, index, level, acc, all) {
    if (acc.length == max || index + level >= arr.length) {
        return;
    }

    acc.push(arr[level + index]);

    for (let i = index; i < arr.length; i++) {
        let newAcc = JSON.parse(JSON.stringify(acc));
        if (level + 1 == max) {
            all.push(acc);
            break;
        }
        dfs(arr, max, i, level + 1, newAcc, all);
    }
}

combine(4, 2);
